namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System.Xml.Serialization;
    using SimpleWebServices;
    using SimpleWebServices.Web.UI.Html.Validation;
    using Xunit;

    public sealed class FieldsetFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Fieldset>()
                .DerivesFrom<InnerElements>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IXmlSerializable>()
                .Implements<IValidation>()
                .XmlRoot("fieldset")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Fieldset expected = new Fieldset
            {
                Id = "id",
                Class = "class",
                Language = "fr",
                Title = "title",
                Elements =
                {
                    new TextString("Some text")
                }
            };

            string xml = "<fieldset id='id' class='class' lang='fr' title='title'>Some text</fieldset>";
            Fieldset actual = xml.Deserialize<Fieldset>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<fieldset />".Deserialize<Fieldset>());
        }

        [Fact]
        public void serialize()
        {
            Fieldset obj = new Fieldset
            {
                Id = "id",
                Class = "class",
                Language = "fr",
                Title = "title",
                Elements =
                {
                    new TextString("Some text")
                }
            };

            string xpath = "1=count(/fieldset[@id='id'][@class='class'][@lang='fr'][@title='title'][text()='Some text'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Fieldset());
        }

        [Fact]
        public void ctor_Name()
        {
            Fieldset obj = new Fieldset("id");

            Assert.Equal<Name>("id", obj.Id);
        }

        [Fact]
        public void ctor_Name_string()
        {
            Fieldset obj = new Fieldset("id", "text");

            Assert.Equal<Name>("id", obj.Id);
            Assert.Equal<string>("legend-id", (obj.Elements[0] as Legend).Id);
            Assert.Equal<string>("text", ((obj.Elements[0] as Legend).Elements[0] as TextString).Value);
        }

        [Fact]
        public void ctor_Name_stringNull()
        {
            Fieldset obj = new Fieldset("id", null as string);

            Assert.Equal<Name>("id", obj.Id);
            Assert.Equal<int>(0, obj.Elements.Count);
        }

        [Fact]
        public void ctor_Name_Legend()
        {
            Legend legend = new Legend();

            Fieldset obj = new Fieldset("id", legend);

            Assert.Equal<Name>("id", obj.Id);
            Assert.Same(legend, obj.Elements[0] as Legend);
        }

        [Fact]
        public void prop_IsValid_get_whenNullValidator()
        {
            Assert.True(new Fieldset().IsValid);
        }

        [Fact]
        public void prop_IsValid_get_whenInvalid()
        {
            Fieldset element = new Fieldset
            {
                Validator = new TestFieldsetValidator(false)
            };

            Assert.False(element.IsValid);
        }

        [Fact]
        public void prop_IsValid_get_whenValid()
        {
            Fieldset element = new Fieldset
            {
                Validator = new TestFieldsetValidator(true)
            };

            Assert.True(element.IsValid);
        }

        [Fact]
        public void prop_Validator()
        {
            Assert.True(new PropertyExpectations(typeof(Fieldset).GetProperty("Validator"))
                .IsAutoProperty<IValidate>()
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            Fieldset expected = new Fieldset
            {
                Id = "id",
                Class = "class",
                Language = "fr",
                Title = "title",
                Elements =
                {
                    new TextString("Some text")
                }
            };

            string xml = "<fieldset id='id' class='class' lang='fr' title='title'>Some text</fieldset>";
            Fieldset actual = new Fieldset().Deserialize("fieldset", null, xml) as Fieldset;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }
    }
}